package com.qfedu.dtboot.service.impl;

import com.qfedu.dtboot.dao.SysMenuMapper;
import com.qfedu.dtboot.dao.SysUserMapper;
import com.qfedu.dtboot.entity.SysMenu;
import com.qfedu.dtboot.service.SysMenuService;
import com.qfedu.dtboot.utils.Constant;
import com.qfedu.dtboot.utils.DataGridResult;
import com.qfedu.dtboot.utils.Query;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @author 水杯
 * @date 2020-07-15 22:05
 */
@Service
public class SysMenuServiceImpl implements SysMenuService {

    @Autowired
    private SysMenuMapper sysMenuMapper;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Override
    public List<SysMenu> queryListAll() {
        return sysMenuMapper.queryListAll();
    }

    @Override
    public DataGridResult getPageList(Query query) {
        List<SysMenu>rows=sysMenuMapper.queryList(query);
        int total = sysMenuMapper.queryTotal(query);
        DataGridResult dataGridResult = new DataGridResult(rows, total);
        return dataGridResult;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void deleteBatch(Long[] menuIds) {
        sysMenuMapper.deleteBatch(menuIds);

    }

    @Override
    public List<SysMenu> queryNotButtonList() {
        return sysMenuMapper.queryNotButtonList();
    }

    @Override
    public void save(SysMenu menu) {
        sysMenuMapper.insertSelective(menu);
    }

    @Override
    public SysMenu queryObject(Long menuId) {
        return sysMenuMapper.selectByPrimaryKey(menuId);
    }

    @Override
    public void update(SysMenu menu) {
        sysMenuMapper.updateByPrimaryKeySelective(menu);
    }

    @Override
    public Set<String> getUserPermissions(long userId) {
        List<String> permsList = null;
        //超级用户（id=1的用户），拥有最高权限
        if(userId == Constant.SUPER_ADMIN){
            List<SysMenu> menuList = queryListAll();
            permsList = new ArrayList<String>(menuList.size());
            for(SysMenu menu : menuList){
                permsList.add(menu.getPerms());
            }
        }else{
            permsList = sysMenuMapper.queryAllPerms(userId);
        }

        //用户权限列表
        Set<String> permsSet = new HashSet<String>();
        for(String perms : permsList){
            if(StringUtils.isBlank(perms)){
                continue;
            }
            permsSet.addAll(Arrays.asList(perms.trim().split(",")));
        }
        return permsSet;
    }

    /**
     * 获取当前用户有权限的菜单ID,父节点parentId为多少的有用户权限的菜单ID
     * @param parentId
     * @param menuIdList
     * @return
     */
    private List<SysMenu> queryListParentId(Long parentId,List<Long> menuIdList)
    {
        //根据父ID获取子菜单列表
          List<SysMenu> menuList=sysMenuMapper.queryListParentId(parentId);
            if(menuIdList==null)
            {
                //如果为空返回所有菜单权限
                 return  menuList;
            }
            //过滤当前用户有权访问的子菜单列表
            List<SysMenu> userMenuList=new ArrayList<SysMenu>();
            for(SysMenu menu:menuList)
            {
                //当前MenuId包含MeunIdList中
                if(menuIdList.contains(menu.getMenuId()))
                {
                    userMenuList.add(menu);
                }
            }
          return userMenuList;
        }

    /**
     * 填充下一级子菜单
     * @param menuList  当前用户可以访问的菜单
     * @param menuIdList  当前用户可访问的菜单Id列表
     */
        private  void  getMenuTreeList(List<SysMenu> menuList,List<Long> menuIdList)
        {
            for(SysMenu menu:menuList)
            {
                //根据菜单ID列表获取当前用户可见的子菜单列表
                List<SysMenu> subMenuList1 = queryListParentId(menu.getMenuId(), menuIdList);
                //递归获取子菜单
                getMenuTreeList(subMenuList1,menuIdList);
                menu.setList(subMenuList1);

            }
        }

    /**
     * 获取当前用户所用菜单
     * @param
     * @return
     */
    private List<SysMenu> getAllMenuList(List<Long> menuIdList)
        {
            //获取顶层节点列表
            List<SysMenu> menuList = queryListParentId(0L, menuIdList);
            //获取第二层菜单
            getMenuTreeList(menuList,menuIdList);
            return menuList;

        }

    /**
     * 当前用户有权限的列表
     * @param userId
     * @return
     */
    @Override
    public List<SysMenu> getUserMenuList(Long userId)
        {
            //如果是超级用户
            if(userId==1)
            {
               return getAllMenuList(null);
            }
            List<Long> meunIdList = sysUserMapper.queryAllMenuId(userId);
           return getAllMenuList(meunIdList);

        }

}
